#!/usr/bin/env python

'''
    Strip HTML tables from SIS into plain ASCII tables.
    This is the first pass in conversion

    Relies on 'w3m' (a text based Web browser and pager)
'''

import os, sys
import subprocess, string

unprintable = []
for c in range(32):
    unprintable.append(chr(c))
for c in range(128,256):
    unprintable.append(chr(c))
unprintable = ''.join(unprintable)
transtable = string.maketrans(unprintable, ' '*len(unprintable))
def printable(l):
    s =  l.translate(transtable)
    while s.find('  ') >= 0:
        s = s.replace('  ', ' ')
    return s.strip()

class FileInfo:
    def __init__(self, file, replaces = []):
        self.HTML = 'sis-' + file + '.html'
        self.TXT = 'sis-' + file + '.txt'
        self.Replace = replaces
        if not os.path.exists(self.HTML):
            print 'ERROR: Missing input file:', self.HTML
            sys.exit(1)

    def stripHTML(self):
        fp = subprocess.Popen('w3m -T text/html %s -dump' % self.HTML,
                                shell=True, stdout=subprocess.PIPE).stdout
        lines = []
        for l in fp.readlines():
            s = printable(l)
            if len(s) and s[0] >= '0' and s[0] <= '9':
                for r in self.Replace:
                    s = s.replace(r[0], r[1])
                lines.append(s)
        open(self.TXT, 'w').write('\n'.join(lines))

files = [
    FileInfo('conflict'),
    FileInfo('request'),
    FileInfo('reviewer-topics', [ ('expert','1'), ('knowledgeable','2')]),
    FileInfo('reviewers'),
    FileInfo('submission-topics'),
    FileInfo('submissions'),
    FileInfo('SRC'),
    FileInfo('topics'),
]

def main():
    print 'Converting HTML into something parseable'
    for f in files:
        f.stripHTML()
    print 'SUCCESS\n'
    print 'Now, run txt2map.py to generate map files'

if __name__ == "__main__":
    main()
